package com.pets.springboot_petshop.controller;

import com.pets.springboot_petshop.entity.Serviceappointment;
import com.pets.springboot_petshop.entity.Shoppingcart;
import com.pets.springboot_petshop.entity.User;
import com.pets.springboot_petshop.entity.UserDTO;
import com.pets.springboot_petshop.pojo.Result;
import com.pets.springboot_petshop.service.ShoppingcartService;
import com.pets.springboot_petshop.service.UserService;
import com.pets.springboot_petshop.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/15 上午11:12 周二
 * @Description: 购物车控制层
 */
@RestController
@RequestMapping("/Shoppingcart")
public class ShoppingcartController {
    @Autowired
    private ShoppingcartService shoppingcartService;
    @Autowired
    private UserService userService;
    //id查询
@GetMapping("select")
private Result select(@RequestParam int id) {
    Shoppingcart shoppingcart = shoppingcartService.select(id);
    if (shoppingcart != null) {
        return Result.success(shoppingcart);
    }else {
        return Result.error("获取失败");
    }
}
//user_id查询
    @GetMapping("selectuser_id")
    private Result selectuser_id(@RequestParam int user_id) {
        List<Shoppingcart> shoppingcart = shoppingcartService.selectuser_id(user_id);
        if (shoppingcart != null) {
            return Result.success(shoppingcart);
        }else {
            return Result.error("获取失败");
        }
    }
    //用户通过用户token进行user_id查询
    @GetMapping("/Userselect")
    public Result Userselect(@RequestHeader(name = "Authorization") String token) {
        // 根据令牌查询相应的用户信息
        Map<String,Object> map = JwtUtil.parseToken(token);
        int user_id = (int) map.get("id");
        System.out.println(" id = " + user_id);
        // 检查用户是否存在
        User user1 = userService.select(user_id);
        if (user1 == null) {
            return Result.error("用户不存在");
        } else {
            // 使用user_id查询购物车信息
            List<Shoppingcart> shoppingcartList = shoppingcartService.selectuser_id(user_id);
            if (shoppingcartList != null && !shoppingcartList.isEmpty()) {
                return Result.success(shoppingcartList);
            } else {
                return Result.error("购物车信息为空或获取失败");
            }
        }
    }
  //product_id查询
  @GetMapping("selectproduct_id")
  private Result selectproduct_id(@RequestParam int product_id) {
      List<Shoppingcart> shoppingcart = shoppingcartService.selectproduct_id(product_id);
      if (shoppingcart != null) {
          return Result.success(shoppingcart);
      }else {
          return Result.error("获取失败");
      }
  }
    @GetMapping("/selectupdate")
    private Result selectupdate(@RequestBody Shoppingcart shoppingcart) {
        Shoppingcart shoppingcart1 = shoppingcartService.selectupdate(shoppingcart);
        if (shoppingcart1 != null) {
            return Result.success(shoppingcart1);
        }else {
            return Result.error("获取失败");
        }
    }
    // 用户购物车check查询
    @GetMapping("/selectcheck")
    private Result selectcheck(@RequestHeader(name = "Authorization") String token) {
        // 根据令牌查询相应的用户信息
        Map<String,Object> map = JwtUtil.parseToken(token);
        int user_id = (int) map.get("id");
        System.out.println(" id = " + user_id);
        // 检查用户是否存在
        User user1 = userService.select(user_id);
        if (user1 == null) {
            return Result.error("用户不存在");
        }else {
            String check = "1";

            // 创建Shoppingcart对象并设置属性
            Shoppingcart shoppingcart = new Shoppingcart();
            shoppingcart.setUser_id(user_id);
            shoppingcart.setCheck(check);

            // 调用服务层方法查询购物车项
            List<Shoppingcart> shoppingcartList = shoppingcartService.selectcheck(shoppingcart);
            if (shoppingcartList != null && !shoppingcartList.isEmpty()) {
                return Result.success(shoppingcartList);
            } else {
                return Result.error("获取失败");
            }
        }
    }

   //查询所有
   @GetMapping("/findAll")
   private Result findAll() {
       List<Shoppingcart> shoppingcart = shoppingcartService.findAll();
       if(shoppingcart == null || shoppingcart.isEmpty()){
           return Result.error("获取失败");
       }else {
           return Result.success(shoppingcart);
       }
   }
    //用户添加
    @PostMapping("add")
    private Result add(@RequestBody Shoppingcart shoppingcart, @RequestHeader(name = "Authorization") String token) {
        // 根据令牌查询相应的用户信息
        Map<String,Object> map = JwtUtil.parseToken(token);
        int user_id = (int) map.get("id");
        System.out.println(" id = " + user_id);
        // 检查用户是否存在
        User user = userService.select(user_id);
        if (user == null) {
            return Result.error("用户不存在");
        } else {
            // 将获取到的用户ID设置为shoppingcart的user_id
            shoppingcart.setUser_id(user_id);

            // 检查数据库中是否已存在相同的user_id和product_id
            Shoppingcart existingShoppingcart = shoppingcartService.selectupdate(shoppingcart);

            if (existingShoppingcart != null) {
                // 如果存在，更新数量
                int currentQuantity = Integer.parseInt(existingShoppingcart.getQuantity()); // 将String转换为int
                int newQuantity = currentQuantity + Integer.parseInt(shoppingcart.getQuantity()); // 增加新的数量

                // 更新Shoppingcart对象的数量和ID
                shoppingcart.setId(existingShoppingcart.getId()); // 设置正确的ID
                shoppingcart.setQuantity(String.valueOf(newQuantity)); // 将更新后的数量转换回String

                // 调用服务层方法更新购物车项的数量
                if (shoppingcartService.updatequantity(shoppingcart) > 0) {
                    return Result.success("数量更新成功");
                } else {
                    return Result.error("数量更新失败");
                }
            } else {
                // 如果不存在，添加新的购物车项
                if (shoppingcartService.add(shoppingcart) > 0) {
                    return Result.success("新增成功");
                } else {
                    return Result.error("新增失败");
                }
            }
        }
    }

  //删除
@PostMapping("delete")
private Result delete(@RequestHeader(name = "Authorization") String token){
    // 根据令牌查询相应的用户信息
    Map<String,Object> map = JwtUtil.parseToken(token);
    int user_id = (int) map.get("id");
    System.out.println(" id = " + user_id);
    // 检查用户是否存在
    User user = userService.select(user_id);
    if (user == null) {
        return Result.error("用户不存在");
    } else{
        if (shoppingcartService.delete(user_id)>0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
}
    //修改数据
    @PostMapping("update")
    private Result update(@RequestBody Shoppingcart shoppingcart){
        if (shoppingcartService.update(shoppingcart)>0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    //修改数量
    @PostMapping("updatequantity")
    private Result updatequantity(@RequestBody Shoppingcart shoppingcart, @RequestHeader(name = "Authorization") String token) {
        // 根据令牌查询相应的用户信息
        Map<String,Object> map = JwtUtil.parseToken(token);
        int user_id = (int) map.get("id");
        System.out.println(" id = " + user_id);
        // 检查用户是否存在
        User user = userService.select(user_id);
        if (user == null) {
            return Result.error("用户不存在");
        } else {
            shoppingcart.setUser_id(user_id); // 设置user_id

            // 调用服务层方法更新购物车项的数量
            if (shoppingcartService.updatequantity(shoppingcart) > 0) {
                return Result.success("修改成功");
            } else {
                return Result.error("修改失败");
            }
        }
    }
    //修改状态
    @PostMapping("updatecheck")
    private Result updatecheck(@RequestParam int id, @RequestHeader(name = "Authorization") String token) {
        // 根据令牌查询相应的用户信息
        Map<String,Object> map = JwtUtil.parseToken(token);
        int userid = (int) map.get("id");
        // 检查用户是否存在
        User user = userService.select(userid);
        if (user == null) {
            return Result.error("用户不存在");
        } else {
            // 首先通过id和user_id查询购物车项
            Shoppingcart shoppingcart = shoppingcartService.select(id);
            if (shoppingcart != null) {
                // 检查购物车项是否存在
                String currentCheck = shoppingcart.getCheck(); // 获取当前的check值
                String newCheck = currentCheck.equals("0") ? "1" : "0"; // 取反check值
                shoppingcart.setCheck(newCheck); // 设置新的check值
                shoppingcart.setUser_id(user.getId()); // 设置user_id

                // 调用服务层方法更新购物车项
                if (shoppingcartService.updatecheck(shoppingcart) > 0) {
                    return Result.success("修改成功");
                } else {
                    return Result.error("修改失败");
                }
            } else {
                return Result.error("购物车项不存在");
            }
        }
    }
    @PostMapping("/updatecheckall")
    public Result updateCheckAll(@RequestHeader(name = "Authorization") String token) {
        // 从令牌中解析用户ID
        Map<String, Object> map = JwtUtil.parseToken(token);
        Integer user_id = (Integer) map.get("id");

        if (user_id == null) {
            return Result.error("用户不存在");
        }

        // 调用服务层方法进行批量更新
        boolean success = shoppingcartService.updateCheckForAll(user_id);
        if (success) {
            return Result.success("全选状态修改成功");
        } else {
            return Result.error("全选状态修改失败");
        }
    }
}
